package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  购物车服务实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 先判断 购物车表中是否存在一模一样的数据
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        // 将用户id注入
        cart.setUserId(BaseContext.getCurrentId());
        // 查询购物车表中是否存在一模一样的数据
        List<ShoppingCart> list = shoppingCartMapper.list(cart);

        if(list != null &&list.size() > 0){
            // 存在则修改数量
            cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else{
            // 不存在则插入数据
            // 先判断这个购物车要添加的数据是菜品还是套餐
            if(shoppingCartDTO.getSetmealId() != null){
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }else{
                Dish dish = dishMapper.getBYId(shoppingCartDTO.getDishId());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);
        }

    }

    /**
     *  查询购物车中所有的商品
     * @return
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        ShoppingCart cart = new ShoppingCart();
        // 获取用户id
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    /**
     * 删除购物车中的数据
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        // 先判断这条数据的条数 大于1 就减一
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        // 将用户id注入
        cart.setUserId(BaseContext.getCurrentId());
        // 查询购物车表中是否存在一模一样的数据
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        cart = list.get(0);
        if(cart.getNumber()> 1){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }else{
            // 不大于1删除
            cart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO,cart);
            shoppingCartMapper.delete(cart);
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(cart);
    }
}
